package com.cuiweiyou.vigilesmicrecord.base;

import android.Manifest;
import android.content.Intent;
import android.support.annotation.NonNull;

import com.cuiweiyou.vigilesmicrecord.util.comm.PermissionTool;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;

/**
 * Created by macpro on 2018/4/18.
 */

@RuntimePermissions
public abstract class IStorageAudioPermissionActivity extends BaseActivity {

    //////////

    public abstract void initView();

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        initPermissions();
    }

    public void initPermissions() {
        // 开始请求权限
        IStorageAudioPermissionActivityPermissionsDispatcher.obtainLocationWithPermissionCheck(IStorageAudioPermissionActivity.this);
    }

    /**
     * 得到权限
     */
    @NeedsPermission({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO})
    public void obtainLocation() {
        initView();
    }

    // 提示为什么需要该权限
    @OnShowRationale({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO})
    public void showRationaleForPermission(final PermissionRequest request) {

        PermissionTool.showPermissionDialog(IStorageAudioPermissionActivity.this, new PermissionTool.OnNeedPermissionAgreeBack() {
            @Override
            public void agree(boolean authorized) {
                if (authorized) {
                    request.proceed(); // 系统申请框
                } else {
                    finish();
                }
            }
        });
    }

    // 被拒绝
    @OnPermissionDenied({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO})
    public void showDeniedForLocation() {
        PermissionTool.showPermissionRejectedDialog(this);
    }

    // 被拒绝并且勾选了不再提醒
    @OnNeverAskAgain({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO})
    public void showNeverAskForLocation() {

        PermissionTool.requestPermissionFromSettingDialog(this);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        IStorageAudioPermissionActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (PermissionTool.FLAG_REQUEST_PERMISSION == requestCode) {
            initPermissions();
        }
    }
}
